//
//  HCFindExpectView.m
//  Aierle
//
//  Created by jiayibai-02por on 2017/6/1.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "HCFindExpectView.h"
#import "HCFindExpectCell.h"

@interface HCFindExpectView()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *expectCollectionView;

@end

@implementation HCFindExpectView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self initSubviews];
    }
    return self;
}

- (void)initSubviews {

    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(10, 0, Screen_width-20, 0.5)];
    line.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:line];
    
    UILabel *leftTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 8, 100, 16)];
    leftTitleLabel.font = [UIFont systemFontOfSize:14];
    leftTitleLabel.textColor = [UIColor blackColor];
    leftTitleLabel.text = @"积分兑换";
    [self addSubview:leftTitleLabel];
    
//    UIButton *rightWholeButton = [[UIButton alloc] initWithFrame:CGRectMake(Screen_width - YBKAdaptiveW(65), 0, YBKAdaptiveW(65), YBKAdaptiveH(35))];
//    [rightWholeButton setTitle:@"全部>" forState:UIControlStateNormal];
//    rightWholeButton.titleLabel.font = [UIFont systemFontOfSize:10];
//    [rightWholeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    [rightWholeButton addTarget:self action:@selector(rightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:rightWholeButton];

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.sectionInset = UIEdgeInsetsMake(0, (Screen_width-YBKAdaptiveW(65)*4)/8, 0, (Screen_width-YBKAdaptiveW(65)*4)/8);
    layout.minimumLineSpacing = (Screen_width-YBKAdaptiveW(65)*4)/4;
    layout.minimumInteritemSpacing = 0;
    layout.itemSize = CGSizeMake(YBKAdaptiveW(65), YBKAdaptiveH(105));
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.expectCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, YBKAdaptiveH(35), Screen_width, YBKAdaptiveH(105)) collectionViewLayout:layout];
    self.expectCollectionView.delegate = self;
    self.expectCollectionView.dataSource = self;
    self.expectCollectionView.backgroundColor = [UIColor whiteColor];
    [self.expectCollectionView registerNib:[UINib nibWithNibName:@"HCFindExpectCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"expect"];
    self.expectCollectionView.showsVerticalScrollIndicator = NO;
    self.expectCollectionView.showsHorizontalScrollIndicator = NO;
    [self addSubview:self.expectCollectionView];
}

- (void)setExpectArray:(NSArray *)expectArray {

    _expectArray = expectArray;
    if (expectArray.count != 0) {
        
        [self.expectCollectionView reloadData];
    }
}

#pragma mark --点击全部按钮
- (void)rightButtonClick:(UIButton *)sender {

    
}

#pragma mark -- UICollectionView delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.expectArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"expect";
    HCFindExpectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    if (!cell) {
        
        cell = [[HCFindExpectCell alloc] initWithFrame:CGRectZero];
    }
    
    if (self.expectArray.count != 0) {
        
        NSString *imageurl = [NSString stringWithFormat:@"%@",[self.expectArray[indexPath.item] objectForKey:@"img"]];
        [cell.expectImage sd_setImageWithURL:[NSURL URLWithString:imageurl] placeholderImage:nil];
        NSString *integralNumString = [NSString stringWithFormat:@"%@",[self.expectArray[indexPath.item] objectForKey:@"price"]];
        cell.integralNum.text = integralNumString;
        NSString *cellName = [NSString stringWithFormat:@"%@",[self.expectArray[indexPath.item] objectForKey:@"name"]];
        cell.nameLabel.text = cellName;
    }

    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    if (self.didClickCellBlock) {
        self.didClickCellBlock([self.expectArray[indexPath.item] objectForKey:@"url"]);
    }
}

@end
